package com.jiantong.web.open.app.lawEnforcing;

import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.lawEnforcing.model.BorrowRecord;
import com.jiantong.lawEnforcing.service.BorrowRecordService;
import com.jiantong.user.model.User;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Map;

/**
 * 借阅记录
 * Created by mz03 on 2017/8/25.
 */
@Slf4j
@OpenBean
public class BorrowRecordApis {
    @RpcConsumer
    private BorrowRecordService borrowRecordService;


    /**
     * 根据id查询一条信息
     *
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.borrowRecord.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public BorrowRecord findById(Long id) {
        Response<BorrowRecord> response = borrowRecordService.finById(id);
        if (!response.isSuccess()) {
            log.error("failed to find news read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 创建一条信息
     *
     * @param borrowRecord
     * @return
     */
    @OpenMethod(
            key = "create.borrowRecord",
            paramNames = {"borrowRecord"},
            httpMethods = RequestMethod.POST
    )
    public Long create(BorrowRecord borrowRecord) {
        Long borrower_id = UserUtil.getUserId();
        borrowRecord.setBorrower_id(borrower_id);
        Response<Long> response = borrowRecordService.create(borrowRecord);
        if (!response.isSuccess()) {
            log.error("failed to create borrowRecord read by borrowRecord = {}, cause : {}", borrowRecord, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 更新
     *
     * @param borrowRecord
     * @return
     */
    @OpenMethod(
            key = "update.borrowRecord",
            paramNames = {"borrowRecord"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(BorrowRecord borrowRecord) {
        if (borrowRecord.getId() == null) {
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = borrowRecordService.update(borrowRecord);
        if (!response.isSuccess()) {
            log.error("failed to update borrowRecord");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    "failed to update borrowRecord");
        }
        return response.getResult();
    }

    /**
     * 分页查询list 用于显示查看我的借阅记录页面
     *
     * @param pageNo
     * @param pageSize
     * @param statusList   申请状态(是否通过)
     * @return 分页
     */
    @OpenMethod(
            key = "find.paging.by.list",
            paramNames = {"pageNo", "pageSize", "status","role"},

            httpMethods = RequestMethod.GET
    )
    public Paging<BorrowRecord> paging(Integer pageNo,
                                       Integer pageSize,
                                       List<Integer> statusList,
                                       Integer role
                                       ) {


        Map<String, Object> map = Maps.newHashMap();
        //当前登录人id
        Long borrower_id = UserUtil.getUserId();
        //根据当前登录人身份判断是否把登录人id添加到条件中区
        if (borrower_id != null) {
            map.put("borrower_id", borrower_id);
        }
        if (role.equals(13)) {
            map.put("borrower_id",null);
        }
        if (statusList != null) {
            map.put("statusList", statusList);
        }

        Response<Paging<BorrowRecord>> response = borrowRecordService.paging(pageNo, pageSize, map);
        if (!response.isSuccess()) {
            log.error("failed to find paging casesBasic status {}, cause : {}", statusList, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 用于查询当前登录人可以看到的文书id列表, 和不可以看到的文书id列表;
     * 查出来一个当前登录人可以看的文书id列表 需要跟当前登录人id和查看的类型(不可看文书/不可以看的文书)
     *
     */
    @OpenMethod(
            key = "find.list.by.status",
            paramNames = {"status"},
            httpMethods = RequestMethod.GET
    )
    private List<Long> findOkListBy(Integer status){

        Long borrower_id = UserUtil.getUserId();
        Map<String, Object> map = Maps.newHashMap();
        if (borrower_id != null) {
            map.put("borrower_id", borrower_id);
        }
        if (status != null) {
            map.put("status", status);
        }
        Response<List<Long>> response =  borrowRecordService.findByStatus(map);
        if (!response.isSuccess()) {
            log.error("failed to find casesIds by status {}, cause : {}", status, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }
}
